package com.umfwechat.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.umfwechat.common.ReqMessage;
import com.umfwechat.common.ResMessage;
import com.umfwechat.common.constant.CommonConsts;
import com.umpay.util.HttpUtil;

/*************************************************************************
 * <br>description :请求Esoopay帮助类
 * @author 李晶
 * @version 1.0 
 *************************************************************************/
public class RequestEsoopayHelper {
	private static final Logger log = LoggerFactory.getLogger(RequestEsoopayHelper.class);;
	/**
	 * <br>description : 调用esoopayhttp请求
	 * @param req
	 * @return
	 * @throws Exception
	 * @author 李晶
	 */ 
//	public static ResMessage requestEsoopay(ReqMessage req, String serviceRest2) throws Exception {
//		//esoopay请求地址，例如：http://10.10.67.72:8099
//		String host =null;
//		//跨境请求CBE_ESOOPAY
//		if(serviceRest2.contains("fxesoopay")){
//		host = StringUtil.trim(PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "cbe_esoopay.rest2.host", ""));
//		}
//		else{
//		host = StringUtil.trim(PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "esopay.rest2.host", ""));
//		}
//		if ("".equals(host)) {
//			throw new Exception("esopay.rest2.host为空");
//		}
//		String url = host + serviceRest2;
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.putAll(req.getReqMap());
//		params.put(CommonConsts.RPID, req.getRpid());
//		params.put(CommonConsts.REQDATE, req.getReqDate());
//		params.put(CommonConsts.REQTIME, req.getReqTime());
//		
//		ResMessage res = new ResMessage();
//		log.info("httpPostForm2Xml请求数据：" + req + ",url=" + url);
//		Map<String, Object> result = HttpUtil.httpPostForm2Xml(url, params);
//		log.info("httpPostForm2Xml返回结果：" + result);
//		res.setRetCode(StringUtil.trim((String) result.get(CommonConsts.RETCODE)));
//		res.setRetMsg(StringUtil.trim((String) result.get(CommonConsts.RETMSG)));
//		if (null != result.get(CommonConsts.NOTIFYDATA) && ((List) result.get(CommonConsts.NOTIFYDATA)).size() != 0){
//			Map<String ,Object> toMerData= (Map<String ,Object>) ((List) result.get(CommonConsts.NOTIFYDATA)).get(0);
//			res.put(CommonConsts.NOTIFYDATA, toMerData);
//		}	
//		return res;
//	}
	
	//上一个方法对于返回结果处理不具有通用性，因此新写下面方法，林泉韬
	public static ResMessage requestEsoopay2(ReqMessage req, String serviceRest2) throws Exception {
		//esoopay请求地址，例如：http://10.10.67.72:8099
		String host = StringUtil.trim(PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "esopay.rest2.host", ""));
		if ("".equals(host)) {
			throw new Exception("esopay.rest2.host为空");
		}
		String url = host + serviceRest2;
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(req.getReqMap());
		params.put(CommonConsts.RPID, req.getRpid());
		params.put(CommonConsts.REQDATE, req.getReqDate());
		params.put(CommonConsts.REQTIME, req.getReqTime());
		
		ResMessage res = new ResMessage();
		log.info("httpPostForm2Xml2请求数据：" + req + ",url=" + url);
		//esoopay超时时间为：200s
		Map<String, Object> result = HttpUtil.httpPostForm2Xml(url, params, null, 205);
		log.info("httpPostForm2Xml2返回结果：" + result);
		res.setRpid((String) result.get(CommonConsts.RPID));
		res.setRetCode((String) result.get(CommonConsts.RETCODE));
		res.setRetMsg((String) result.get(CommonConsts.RETMSG));
		res.put(result);
		return res;
	}
	
	/**
	 * 请求Esopay帮助方法 B2B跨境付款使用
	 * @author Zhang Ao
	 * */
//	public static ResMessage requestEsoopay3(ReqMessage req, String serviceRest2) throws Exception {
//		//esoopay请求地址，例如：http://10.10.67.72:8099
//		String host = StringUtil.trim(PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "esopay.rest3.host", ""));
//		if ("".equals(host)) {
//			throw new Exception("esopay.rest3.host为空");
//		}
//		String url = "http://10.10.77.76:9918/restUserManager/rest2/CardBinSearch/";
//		System.err.println("url="+url);
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.putAll(req.getReqMap());
//		params.put(CommonConsts.RPID, req.getRpid());
//		params.put(CommonConsts.REQDATE, req.getReqDate());
//		params.put(CommonConsts.REQTIME, req.getReqTime());
//		
//		ResMessage res = new ResMessage();
//		log.info("httpPostForm2Xml2请求数据：" + req + ",url=" + url);
//		//esoopay超时时间为：200s
//		Map<String, Object> result = HttpUtil.httpPostForm2Xml(url, params, null, 205);
//		log.info("httpPostForm2Xml2返回结果：" + result);
//		res.setRpid((String) result.get(CommonConsts.RPID));
//		res.setRetCode((String) result.get(CommonConsts.RETCODE));
//		res.setRetMsg((String) result.get(CommonConsts.RETMSG));
//		res.put(result);
//		return res;
//	}
//	public static void main(String[] args){
//		ReqMessage req = ReqMessageUtil.makeReq();
//		try {
//			ResMessage res = requestEsoopay3(req,null);
//			System.out.println(res.getRetMsg());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
